<template>
	<view v-for="(order, index) in orders" class="order-container" hover-class="order-container-hover"
		@click="onOrderItemClicked(order.orderNo)">
		<view class="row-container">
			<text class="order-text">{{order.createTime}}</text>
			<text class="order-text">{{order.orderStatus}}</text>
		</view>
		<view class="row-container">
			<!--商品图片-->
			<image :src="order.productCover" mode="aspectFit" class="product-cover"></image>
			<!--商品信息-->
			<view class="row-container">
				<text class="order-text">一件{{order.productName}}</text>
				<text class="pay-amount">{{order.payAmount}}元</text>
			</view>
		</view>
		<view class="divider"></view>

	</view>
</template>

<script>
	import Order from '../../models/order'
	let tag = "order-list"
	export default {
		onLoad() {
			this.$log.info(tag, "onLoad")
			this.fetchOrderList()
		},
		data() {
			return {
				orders: []
			}
		},
		methods: {
			onOrderItemClicked(orderNo) {
				this.$log.info(tag, `onOrderItemClicked ${orderNo}`)
				uni.navigateTo({
					url: `/pages/order/order?orderNo=${orderNo}`
				})
			},
			parseFromJsonArray(jsonArray) {
				this.$log.info(tag, ` ${jsonArray.length}`);
				let orders = [];
				for (var i = jsonArray.length - 1; i >= 0; i--) {
					let jsonObject = jsonArray[i];
					orders.push(new Order(
						jsonObject.orderNo,
						jsonObject.createTime,
						jsonObject.productName,
						`${this.constant.BASE_URL}${jsonObject.productCover}`,
						jsonObject.orderStatus,
						jsonObject.payAmount
					));
				}
				return orders;
			},
			fetchOrderList() {
				this.$log.info(tag, "fetchOrderList")

				uni.request({
					url: `${this.constant.BASE_URL}/api/order/list`,
					method: 'GET',
					header: {
						'Authorization': this.loginManager.getToken()
					},
					success: (res) => {
						//this.$log.info(tag, JSON.stringify(res.data))
						console.log(res.data)
						if (res.data.code == 200) {
							this.orders = this.parseFromJsonArray(res.data.rows);
							this.$log.info(tag, `Success parsed orders ${this.orders.length}`)
						}
					},
					fail: (err) => {
						this.$log.error(err.errMsg)
					}
				})
			}
		}
	}
</script>

<style>
	.order-container {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.row-container {
		width: 90%;
		margin-top: 5px;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
	}

	.product-cover {
		width: 70px;
		height: 70px;
	}

	.product-info-container {
		display: flex;
		flex-direction: column;
		justify-content: space-evenly;
		align-items: flex-end;
	}

	.order-text {
		color: dimgrey;
		font-size: 36rpx;
	}

	.pay-amount {
		color: black;
		font-weight: bold;
		font-style: 36rpx;
	}

	.divider {
		width: 100%;
		height: 1px;
		background-color: lightgray;
		margin-top: 5px;
		margin-bottom: 5px;
	}

	.order-container-hover {
		opacity: 0.5;
	}
</style>